package cn.itsource.org.service.impl;

import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.exception.ErrorCode;
import cn.itsource.basic.page.PageList;
import cn.itsource.basic.page.Pageable;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.service.IDepartmentService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;





    @Override
    @CacheEvict(cacheNames = "dept",key = "'tree'")
    public void saveOrUpdate(Department department) {
        if(department.getId() != null){
            departmentMapper.update(department);
        }else {
            departmentMapper.insert(department);
            if(department.getParent() == null){
                //没有选上级部门
                department.setDirPath("/"+department.getId());
            }else{
                //选择了上级部门
                Department dadDept = findOne(department.getParent().getId());
                department.setDirPath(dadDept.getDirPath()+"/"+department.getId());
            }
            departmentMapper.update(department);
        }
    }

    @Override
    @CacheEvict(cacheNames = "dept",key = "'tree'")
    public void delete(Long id) {
        departmentMapper.delete(id);
    }


    @Override
    @CacheEvict(cacheNames = "dept",key = "'tree'")
    public void bachDelete(Map<String,String> map) {
        String ids = map.get("ids");
        String[] split = ids.split(",");

        List<Long> list = Arrays.stream(split).map(id -> Long.valueOf(id)).collect(Collectors.toList());
        departmentMapper.batchDelete(list);
    }

    @Override
    @Cacheable(cacheNames = "dept",key = "'tree'")
    public List<Department> tree() {
        //查询出所有的dept
        List<Department> depts = departmentMapper.loadAll();

        //把所有数据转换成map，key是自己的id，value是自己这个对象
        Map<Long, Department> map = depts.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        //结果集
        List<Department> results = new ArrayList<>();
        for (Department dept : depts) {
            //找出所有的顶级部门
            if(dept.getParentId() == null){
                //把顶级部门装到集合中
                results.add(dept);
            }else{
                //这里的dept就是有爹的部门
//                for (Department dadDept : depts) {
//                    if(dept.getParentId() == dadDept.getId()){
//                        if(dadDept != null){
//                            //找到dept的爹，把自己装到爹的儿子里去
//                            dadDept.getChildren().add(dept);
//                        }
//
//                    }
//                }
                //找爹
                Department dadDept = map.get(dept.getParentId());
                if(dadDept != null){
                    dadDept.getChildren().add(dept);
                }

            }
        }
        return results;
    }

}
